package jdbc;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/**
 * 关联查询
 */
public class JDBCDemo6 {
    public static void main(String[] args) {
        //需求：王克晶所带班级的信息，列出老师的名字，性别，班级名，所在楼层
        /**
         *SELECT t.name,t.gender,c.name,c.floor
         * FROM teacher t,class c
         * WHERE t.id = c.teacher_id
         * AND t.username = '王克晶'
         */
        //1.建立连接
        try(
                Connection conn = DBUtil.getConnection();
        ) {
            //2.创建Statement对象
            Statement state = conn.createStatement();
            //3.执行SQL语句
            String sql = "SELECT t.name,t.gender,c.name,c.floor" +
                    " FROM teacher t,class c" +
                    " WHERE t.id = c.teacher_id" +
                    " AND t.name = '王克晶'";
            //4.处理结果
            ResultSet rs = state.executeQuery(sql);
            while (rs.next()){
                String name = rs.getString("name");
                String gender = rs.getString("gender");
                String classname = rs.getString("c.name");
                String floor = rs.getString("c.floor");
                System.out.println(name+"--"+gender+"--"+classname+"--"+floor);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
